/*
 * DateTime.cpp
 *
 *  Created on: 2011-11-17
 *      Author: kaishui
 */

#include "DateTime.h"

int DateTime::GetCurDate()
{
	time_t t=time(NULL);
	struct tm * tmt=localtime(&t);
	tmt->tm_hour=0;
	tmt->tm_min=0;
	tmt->tm_sec=0;

	return mktime(tmt);
}


string DateTime::GetNowTime()
{
	time_t t=time(NULL);
	struct tm * tmt=localtime(&t);
	char stime[30];
	string strTime;
	sprintf(stime,"%02d:%02d:%02d ",tmt->tm_hour,tmt->tm_min,tmt->tm_sec);
	strTime.append(stime);
    return strTime;
}

string DateTime::GetTmtTime(time_t t)
{
	struct tm * tmt=localtime(&t);
	char stime[30];
	string strTime;
	sprintf(stime,"%02d:%02d:%02d ",tmt->tm_hour,tmt->tm_min,tmt->tm_sec);
	strTime.append(stime);
    return strTime;
}

string DateTime::GetNowDate()
{
	time_t t=time(NULL);
	struct tm * tmt=localtime(&t);
	char stime[30];
	string strTime;
	sprintf(stime,"%4d-%02d-%02d",tmt->tm_year+1900,tmt->tm_mon+1,tmt->tm_mday);
	strTime.append(stime);
	return strTime;
}

string DateTime::GetTmtDate(time_t t)
{
	struct tm * tmt=localtime(&t);
	char stime[30];
	string strTime;
	sprintf(stime,"%4d-%02d-%02d",tmt->tm_year+1900,tmt->tm_mon+1,tmt->tm_mday);
	strTime.append(stime);
	return strTime;
}


string DateTime::GetNowDateTime()
{
	time_t t=time(NULL);
	struct tm * tmt=localtime(&t);
	char stime[30];
	string strTime;
	sprintf(stime,"%4d-%02d-%02d %02d:%02d:%02d ",tmt->tm_year+1900,tmt->tm_mon+1,tmt->tm_mday,tmt->tm_hour,tmt->tm_min,tmt->tm_sec);
	strTime.append(stime);
	return strTime;
}


int DateTime::str2time(string strTime)
{
	struct tm tmt;
	int ret=sscanf(strTime.c_str(),"%d-%d-%d %d:%d:%d",&(tmt.tm_year),&(tmt.tm_mon),&(tmt.tm_mday),&(tmt.tm_hour),&(tmt.tm_min),&(tmt.tm_sec));
	if(ret>=3)
	{
		tmt.tm_year-=1900;
		tmt.tm_mon-=1;
		if(ret==3)
		{
			tmt.tm_hour=0;
			tmt.tm_min=0;
			tmt.tm_sec=0;
		}
		tmt.tm_isdst=0;
	}
	else
		return 0;
	return mktime(&tmt);
}

string DateTime::time2str(int time,bool bDate)
{
    time_t t=(time_t)time;
	struct tm * tmt=localtime(&t);
	char stime[30];
	string strTime;
	if(bDate)
		sprintf(stime,"%4d-%02d-%02d",tmt->tm_year+1900,tmt->tm_mon+1,tmt->tm_mday);
	else
		sprintf(stime,"%4d-%02d-%02d %02d:%02d:%02d",tmt->tm_year+1900,tmt->tm_mon+1,tmt->tm_mday,tmt->tm_hour,tmt->tm_min,tmt->tm_sec);
	strTime.append(stime);
	return strTime;
}

